//
//  COWelcomeBackAlert.swift
//  CocoProject
//
//  Created by Vill on 2023/11/16.
//

import UIKit

class COWelcomeBackAlert: UIView {

    lazy var background: UIView = {
        let img = UIView(frame: self.bounds)
        img.backgroundColor = UIColor.colorWithHex("#000000", 0.5)
        return img
    }()
    
    lazy var container: UIView = {
        let img = UIView()
        img.backgroundColor = UIColor.clear
        return img
    }()
    
    lazy var bottomImg: UIImageView = {
        let img = UIImageView()
        img.image = KImaged("coco_bind_hands")
        img.contentMode = .scaleAspectFill
        img.layer.masksToBounds = true
        return img
    }()
    
    lazy var giftImg: UIImageView = {
        let img = UIImageView()
        img.image = KImaged("coco_bind_gift")
        return img
    }()
    
    lazy var detailLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.pingfangFont(ofSize: PTW(16), weight: .heavy)
        lbl.textAlignment = .left
        lbl.textColor = .white
        lbl.numberOfLines = 0
        lbl.text = "Long time no see, we give you \n20 coins, have fun！"
        return lbl
    }()
    
    lazy var titleImg: UIImageView = {
        let img = UIImageView()
        img.image = KImaged("coco_bind_welcome")
        return img
    }()
    
    lazy var tipLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.pingfangFont(ofSize: PTW(13), weight: .regular)
        lbl.textAlignment = .center
        lbl.textColor = UIColor.colorWithHex("#FF4D95")
        lbl.numberOfLines = 0
        lbl.text = "（Complimentary coins expire in 2 days）"
        return lbl
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func showInView(_ view:UIView?){
        NotificationCenter.default.post(name: .RandomTimeRunNotification, object: nil,userInfo: ["type":1])
        if (view == nil){
            UIApplication.shared.getKeyWindow()?.addSubview(self)
        }else{
            view?.addSubview(self)
        }
        UIView.animate(withDuration: 0.3) {
            self.container.mj_y = 0
        }
    }
    
    func setUI(){
        addSubview(self.background)
        addSubview(self.container)
        self.container.addTap {[weak self] ges in
            self?.bgClick()
        }
        self.container.addSubview(self.bottomImg)
        self.container.addSubview(self.giftImg)
        self.container.addSubview(self.titleImg)
        self.container.addSubview(self.detailLbl)
        self.container.addSubview(self.tipLbl)
        self.container.snp.makeConstraints { make in
            make.edges.equalTo(self.background)
        }
        self.bottomImg.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(self.background)
            make.height.equalTo(PTW(188))
        }
        self.giftImg.snp.makeConstraints { make in
            make.size.equalTo(PTW(297))
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-PTW(90))
        }
        self.titleImg.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(self.giftImg.snp.top).offset(PTW(2))
        }
        self.detailLbl.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(self.giftImg.snp.bottom).offset(-PTW(36))
        }
        self.tipLbl.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(self.titleImg.snp.bottom).offset(PTW(15))
        }
    }
    
    func bgClick(){
        dismiss()
    }
    
    func dismiss(){
        NotificationCenter.default.post(name: .RandomTimeRunNotification, object: nil,userInfo: ["type":0])
        UIView.animate(withDuration: 0.3) {
            self.container.mj_y = SCREENHEIGHT
        } completion: { finish in
            self.removeFromSuperview()
        }

    }

}
